# ui_schedule_item.py 会自动生成，这里展示修改后的 ScheduleItem 类
from PySide2.QtCore import Signal
from ui_schedule_item import Ui_ScheduleItem  # 生成的UI类

class ScheduleItem(QFrame):
    deleteClicked = Signal(int)
    toggleClicked = Signal(int)
    
    def __init__(self, schedule_id, name, date, time, limit, repeat, enabled=True, parent=None):
        super().__init__(parent)
        self.ui = Ui_ScheduleItem()
        self.ui.setupUi(self)
        self.schedule_id = schedule_id
        
        # 初始化UI数据
        self.ui.lblName.setText(name)
        self.ui.lblDate.setText(f"日期: {date}")
        self.ui.lblTime.setText(f"时间: {time}")
        self.ui.lblLimit.setText(f"每日上限: {limit}人")
        self.ui.lblRepeat.setText(f"重复: {self._get_repeat_text(repeat)}")
        
        # 设置状态标签
        self.set_status(enabled)
        
        # 连接信号
        self.ui.btnDelete.clicked.connect(lambda: self.deleteClicked.emit(self.schedule_id))
        self.ui.btnToggle.clicked.connect(lambda: self.toggleClicked.emit(self.schedule_id))
        
        # 初始样式
        self.setStyleSheet("""
            ScheduleItem {
                border-left: 3px solid #4CAF50;
                background-color: #f8f9fa;
                padding: 8px;
                border-radius: 0 4px 4px 0;
                margin-bottom: 5px;
            }
        """)
    
    def _get_repeat_text(self, repeat):
        repeat_texts = {
            "daily": "每天",
            "weekdays": "工作日",
            "weekends": "周末",
            "once": "仅一次"
        }
        return repeat_texts.get(repeat, repeat)
    
    def set_status(self, enabled):
        self.ui.btnToggle.setIcon(QIcon.fromTheme(
            "media-playback-start" if enabled else "media-playback-stop"
        ))
        self.ui.lblStatus.setText("已启用" if enabled else "已禁用")
        self.ui.lblStatus.setStyleSheet(f"""
            padding: 2px 5px;
            border-radius: 3px;
            background-color: {'#d1fae5' if enabled else '#f3f4f6'};
            color: {'#065f46' if enabled else '#4b5563'};
        """)